package com.wanbatv.wangwangba.model.imple;

import android.content.Context;

import com.wanbatv.wangwangba.model.OnMainListener;
import com.wanbatv.wangwangba.model.OnPayListener;
import com.wanbatv.wangwangba.model.PayModel;
import com.wanbatv.wangwangba.model.entity.AuthenticationData;
import com.wanbatv.wangwangba.model.entity.OrderData;
import com.wanbatv.wangwangba.model.entity.PayData;
import com.wanbatv.wangwangba.util.Config;
import com.wanbatv.wangwangba.util.GitPayService;
import com.wanbatv.wangwangba.util.HeaderClient;
import com.wanbatv.wangwangba.util.Product;
import com.wanbatv.wangwangba.util.Util;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 俞亚楠 on 2016/7/28.
 */
public class PayModelImple implements PayModel {
    Context context;
    public PayModelImple(Context context){
        this.context=context;
    }
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl(Config.httpUrl())
            .addConverterFactory(GsonConverterFactory.create())
            .client(HeaderClient.genericClient())
            .build();
    GitPayService api=retrofit.create(GitPayService.class);
    @Override
    public void setAuthentication(final OnPayListener onPayListener) {
        api.getAuthentication(Product.appId, Util.getDeviceId(context)).enqueue(new Callback<AuthenticationData>() {
            @Override
            public void onResponse(Call<AuthenticationData> call, Response<AuthenticationData> response) {
                boolean payCompleted=response.body().getData().isPayCompleted();
                onPayListener.onloadAuthenticationData(payCompleted);
            }

            @Override
            public void onFailure(Call<AuthenticationData> call, Throwable t) {

            }
        });
    }

    @Override
    public void setPay(final OnPayListener onPayListener,final int monthoryear) {
        api.getPay(Product.appId,Product.flavor).enqueue(new Callback<PayData>() {
            @Override
            public void onResponse(Call<PayData> call, Response<PayData> response) {
                List<PayData.DataBean.ListBean> listBeen=response.body().getData().getList();
                onPayListener.onloadPayData(listBeen,monthoryear);
            }

            @Override
            public void onFailure(Call<PayData> call, Throwable t) {

            }
        });
    }

    @Override
    public void setOrder(final OnPayListener onPayListener, final String subject, final String subjectId, final String price, String provider) {
        api.getorder("1156",subject,subjectId,price,provider,Util.getDeviceId(context),Product.flavor)
                .enqueue(new Callback<OrderData>() {
                    @Override
                    public void onResponse(Call<OrderData> call, Response<OrderData> response) {
                        String order=response.body().getData().getOrder();
                        onPayListener.onloadOrder(subject,subjectId,price,order);
                    }

                    @Override
                    public void onFailure(Call<OrderData> call, Throwable t) {

                    }
                });
    }
}
